
#include  <msp430x54xA.h>


enum I2C_Status
{
  None,
  Done,
  I2cWriteFail,
  I2cReadFail,
  I2cBusUnavailable,
  NoRespondence,
  I2cDataLengthFail,
  I2cPECFail,
  I2cAlFail
};

extern unsigned char CRC8LookUp[];
unsigned char Calculate_CRC8(unsigned char CRC_Pre_Result, unsigned char DataByte);

#define TRX_DataBuffer          100
#define RX_Buffer_SIZE      40
#define DEVICE_ADDRESS      0x0B  // for master address
#define OWN_ADDRESS         0x0B // for slave Address
#define RETRY_COUNT         3
#define WAIT_COUNT          100


/////////////////////////////////////
//Assign I2C pins to USCI_B0
//P3.1:SDA, P3.2:SCL
//////////////////////////////////////////
extern volatile unsigned char RxBuffer_UCB0[];

void Init_USCI_B0_IIC();
void Enable_TX_interrupt_USCI_B0_IIC();
void Enable_RX_interrupt_USCI_B0_IIC();
char MasterIIC_Write_UCB0(unsigned char *PTxData, unsigned char TXByteCtr);
char MasterIIC_Read_UCB0(unsigned char *PRxData,unsigned char RXByteCtr);
char IIC_WR0(unsigned char *PTxData, unsigned char TXByteCtr, unsigned char *PRxData,unsigned char RXByteCtr, unsigned char retryCount);

////////////////////////////////////////////////////////////////////////////
//EEPROM Control configuration 
// P2.5 WP control
////////////////////////////////////////////////////////////////////////////
#define EEPROM_ADDRESS         0x50 // FOR Page 0 Address

#define EEPROM_WP_PORT_OUT      P2OUT
#define EEPROM_WP_PORT_DIR      P2DIR
#define EEPROM_WP_PORT_SEL      P2SEL
#define EEPROM_WP               BIT5

void Init_EEPROM();
void setEEPROMRewritable(char enable);
char EEPROM_Write(unsigned int nAddress, unsigned char *PTxData, char dataCount);
char EEPROM_Read(unsigned int nAddress, unsigned char *PRxData, char dataCount);

/*
//////////////////////////////////////////////
// For EEPROM 8K, BR24S08
//////////////////////////////////////////////
enum EEPROM_PageNum{
  _P0,
  _P1,
  _P2,
  _P3  
};
void Init_EEPROM(char PageNum);
void setEEPROMRewritable(char enable);
char EEPROM_Write(char nAddress, unsigned char *PTxData, char dataCount);
char EEPROM_Read(char nAddress, unsigned char *PRxData, char dataCount);
*/


///////////////////////////////////////////
//Assign I2C pins to USCI_B1
//P3.7:SDA, P5.4:SCL
//////////////////////////////////////////
extern volatile unsigned char RxBuffer_UCB1[];

void Init_USCI_B1_IIC();
void Enable_TX_interrupt_USCI_B1_IIC();
void Enable_RX_interrupt_USCI_B1_IIC();
char MasterIIC_Write_UCB1(unsigned char *PTxData, unsigned char TXByteCtr);
char MasterIIC_Read_UCB1(unsigned char *PRxData,unsigned char RXByteCtr);
char IIC_WR1(unsigned char *PTxData, unsigned char TXByteCtr, unsigned char *PRxData,unsigned char RXByteCtr, unsigned char retryCount);




///////////////////////////////////////////
//Assign I2C pins to USCI_B2
//P9.1:SDA, P9.2:SCL
//////////////////////////////////////////
extern volatile unsigned char RxBuffer_UCB2[];

void Init_USCI_B2_IIC();
void Enable_TX_interrupt_USCI_B2_IIC();
void Enable_RX_interrupt_USCI_B2_IIC();
char MasterIIC_Write_UCB2(unsigned char *PTxData, unsigned char TXByteCtr);
char MasterIIC_Read_UCB2(unsigned char *PRxData,unsigned char RXByteCtr);
char IIC_WR2(unsigned char *PTxData, unsigned char TXByteCtr, unsigned char *PRxData,unsigned char RXByteCtr, unsigned char retryCount);


///////////////////////////////////////////
//Assign I2C pins to USCI_B3
//P10.1:SDA, P10.2:SCL
//////////////////////////////////////////
//extern volatile unsigned char RxBuffer_UCB3[];
#define MAX_TRANSMIT_COUNT 100
#define MAX_RECEIVE_COUNT 32

void Init_USCI_B3_IIC(char ownAddress);
extern void getSlaveTransmitData(unsigned char command);
extern unsigned char TxBuffer_UCB3[MAX_TRANSMIT_COUNT];       // Allocate XXX byte of RAM

#define MAX_TX_Buffer_Count 5
#define MAX_TX_Buffer_Size 6
static unsigned char TX_Buffer_Index;

//unsigned char TxBufferData[];




char SMBUS0_WriteWord_PEC(unsigned char command, unsigned char *WriteData);
char SMBUS0_ReadWord_PEC(unsigned char command, unsigned char *ReadData);
char SMBUS0_WriteBlock_PEC(unsigned char command, unsigned char *WriteData, unsigned char DataLength );
char SMBUS0_ReadBlock_PEC(unsigned char command, unsigned char *ReadData, unsigned char *DataLength);


char SMBUS1_WriteWord_PEC(unsigned char command, unsigned char *WriteData);
char SMBUS1_ReadWord_PEC(unsigned char command, unsigned char *ReadData);
char SMBUS1_WriteBlock_PEC(unsigned char command, unsigned char *WriteData, unsigned char DataLength );
char SMBUS1_ReadBlock_PEC(unsigned char command, unsigned char *ReadData, unsigned char *DataLength);

char SMBUS2_WriteWord_PEC(unsigned char command, unsigned char *WriteData);
char SMBUS2_ReadWord_PEC(unsigned char command, unsigned char *ReadData);
char SMBUS2_WriteBlock_PEC(unsigned char command, unsigned char *WriteData, unsigned char DataLength );
char SMBUS2_ReadBlock_PEC(unsigned char command, unsigned char *ReadData, unsigned char *DataLength);

